﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using DTO;

namespace DAL
{
   public class TINH_TPDAL: KetNoiDuLieu
    {
        public void Insert(TINH_TP tinhtp)
        {
            SqlCommand cm = new SqlCommand("sp_tinhtp_Insert",LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TEN_TP", tinhtp.Ten_tp);
            cm.ExecuteNonQuery();

        }
        public void Update(TINH_TP tinhtp)
        {
            SqlCommand cm = new SqlCommand("sp_tinhtp_Update",LayKetNoi()); 
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_TINH", tinhtp.Ma_tinh);
            cm.Parameters.AddWithValue("@TEN_TP", tinhtp.Ten_tp);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Matinh)
        {
            SqlCommand cm = new SqlCommand("sp_tinhtp_Delete",LayKetNoi());
  
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_TINH", Matinh);
            cm.ExecuteNonQuery();
        }
        public tinh_tpColection GET(string Where, int PageIndex, int PageSize, string OrderBy,
                                        string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tinhtp_GET",LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);

            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);


            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);

            tinh_tpColection tinh_tpColection = new tinh_tpColection();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                TINH_TP tinhtp = new TINH_TP();
                tinhtp.Ma_tinh = int.Parse(ds.Tables[0].Rows[i]["MA_TINH"].ToString());
                tinhtp.Ten_tp = ds.Tables[0].Rows[i]["TEN_TP"].ToString();
                tinh_tpColection.Add(tinhtp);

            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return tinh_tpColection;
        }

    }
}
